package src.com.doit.demo02;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Test_Map {
    public static void main(String[] args) {
        Map<String,Integer> has = new HashMap<>();
        has.put("金莲",12);
        has.put("西门庆",112);
        has.put("武松",23);
        has.put("大郎",122);

        has.putIfAbsent("金莲",21);//putIfAbsent

//        has.putAll("金莲",21);
        Set<String> strings = has.keySet();
        //获取的方式是根据"键"来找对应的"值"
        Iterator<String> i = strings.iterator();//迭代器遍历
        while (i.hasNext())
        {
            String key = i.next();
            System.out.println(key+"♥"+has.get(key));
        }
        System.out.println("------------------");
        for (String key:strings)//增强for遍历
        {
            System.out.println(key+"="+has.get(key));
        }

        System.out.println("+++++++++++++++++++++++++");

        //根据键值对
        Set<Map.Entry<String, Integer>> set = has.entrySet();

        Iterator<Map.Entry<String, Integer>> it = set.iterator();
        while(it.hasNext())
        {
            Map.Entry<String, Integer> next = it.next();
            System.out.println(next.getKey()+" "+next.getValue());
        }
        System.out.println("=======================");
        for (Map.Entry<String,Integer> next:set)
        {
            System.out.println(next.getKey()+" "+next.getValue());
        }
    }
}
